<template>
  <div class="register-container">
    <div class="register-form">
      <h2>创建新账户</h2>
      <p class="subtitle">加入AI角色扮演平台，开始您的对话之旅</p>
      
      <!-- 错误信息显示 -->
      <div v-if="authStore.error" class="error-message">
        {{ authStore.error }}
      </div>
      
      <!-- 成功信息显示 -->
      <div v-if="successMessage" class="success-message">
        {{ successMessage }}
      </div>
      
      <!-- 注册表单 -->
      <form @submit.prevent="handleRegister">
        <div class="form-group">
          <label for="username">用户名</label>
          <input
            type="text"
            id="username"
            v-model="username"
            required
            placeholder="请输入用户名"
            class="form-input"
          />
        </div>
        
        <div class="form-group">
          <label for="email">邮箱</label>
          <input
            type="email"
            id="email"
            v-model="email"
            required
            placeholder="请输入邮箱"
            class="form-input"
          />
        </div>
        
        <div class="form-group">
          <label for="password">密码</label>
          <input
            type="password"
            id="password"
            v-model="password"
            required
            placeholder="请设置密码（至少6位）"
            class="form-input"
          />
          <p class="password-hint">密码至少包含6个字符</p>
        </div>
        
        <div class="form-group">
          <label for="confirmPassword">确认密码</label>
          <input
            type="password"
            id="confirmPassword"
            v-model="confirmPassword"
            required
            placeholder="请再次输入密码"
            class="form-input"
          />
          <div v-if="password !== confirmPassword && confirmPassword" class="password-match-error">
            两次输入的密码不一致
          </div>
        </div>
        
        <div class="form-options">
          <label class="checkbox-label">
            <input type="checkbox" v-model="agreeTerms" required />
            <span>我已阅读并同意<a href="#" class="terms-link">服务条款</a>和<a href="#" class="privacy-link">隐私政策</a></span>
          </label>
        </div>
        
        <button
          type="submit"
          :disabled="authStore.loading || password !== confirmPassword || !agreeTerms"
          class="submit-btn"
        >
          {{ authStore.loading ? '注册中...' : '注册' }}
        </button>
      </form>
      
      <!-- 登录链接 -->
      <div class="login-link">
        已有账户？ <router-link to="/login">立即登录</router-link>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { authStore } from '../store/auth';

const router = useRouter();
const authStoreRef = authStore();

// 表单数据
const username = ref('');
const email = ref('');
const password = ref('');
const confirmPassword = ref('');
const agreeTerms = ref(false);
const successMessage = ref('');

// 处理注册
const handleRegister = async () => {
  // 重置成功信息
  successMessage.value = '';
  
  // 检查密码是否一致
  if (password.value !== confirmPassword.value) {
    authStoreRef.error = '两次输入的密码不一致';
    return;
  }
  
  try {
    await authStoreRef.register({
      username: username.value,
      email: email.value,
      password: password.value
    });
    
    // 注册成功后显示提示信息并清空表单
    successMessage.value = '注册成功！请登录您的账户。';
    username.value = '';
    email.value = '';
    password.value = '';
    confirmPassword.value = '';
    agreeTerms.value = false;
    
    // 3秒后自动跳转到登录页面
    setTimeout(() => {
      router.push({ name: 'login' });
    }, 3000);
  } catch (error) {
    // 错误处理已在auth store中完成
    console.error('Registration failed:', error);
  }
};

// 组件挂载时
const mounted = () => {
  // 如果用户已经登录，重定向到首页
  if (authStoreRef.isAuthenticated) {
    router.push({ name: 'home' });
  }
};

// 调用挂载函数
mounted();
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.register-form {
  background-color: white;
  padding: 40px;
  border-radius: 10px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
  width: 100%;
  max-width: 450px;
}

.register-form h2 {
  margin-top: 0;
  margin-bottom: 10px;
  font-size: 2rem;
  color: #333;
  text-align: center;
}

.subtitle {
  text-align: center;
  color: #666;
  margin-bottom: 30px;
}

.error-message {
  background-color: #fed7d7;
  color: #c53030;
  padding: 12px;
  border-radius: 5px;
  margin-bottom: 20px;
  text-align: center;
}

.success-message {
  background-color: #c6f6d5;
  color: #276749;
  padding: 12px;
  border-radius: 5px;
  margin-bottom: 20px;
  text-align: center;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: 600;
  color: #333;
}

.form-input {
  width: 100%;
  padding: 12px 15px;
  border: 2px solid #e0e0e0;
  border-radius: 5px;
  font-size: 1rem;
  transition: border-color 0.3s ease;
  box-sizing: border-box;
}

.form-input:focus {
  outline: none;
  border-color: #667eea;
}

.password-hint {
  margin-top: 5px;
  font-size: 0.85rem;
  color: #666;
}

.password-match-error {
  color: #c53030;
  font-size: 0.85rem;
  margin-top: 5px;
}

.form-options {
  margin-bottom: 25px;
}

.checkbox-label {
  display: flex;
  align-items: flex-start;
  cursor: pointer;
  color: #666;
  font-size: 0.9rem;
}

.checkbox-label input {
  margin-right: 8px;
  margin-top: 2px;
}

.terms-link,
.privacy-link {
  color: #667eea;
  text-decoration: none;
}

.terms-link:hover,
.privacy-link:hover {
  text-decoration: underline;
}

.submit-btn {
  width: 100%;
  padding: 12px;
  background-color: #667eea;
  color: white;
  border: none;
  border-radius: 5px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.submit-btn:hover:not(:disabled) {
  background-color: #5a67d8;
}

.submit-btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.login-link {
  text-align: center;
  margin-top: 20px;
  color: #666;
}

.login-link a {
  color: #667eea;
  text-decoration: none;
  font-weight: 600;
}

.login-link a:hover {
  text-decoration: underline;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .register-form {
    padding: 30px 20px;
  }
  
  .register-form h2 {
    font-size: 1.75rem;
  }
}
</style>